package com.shaoshaohuo.app.ui;


import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.framework.ActivityCollector;
import com.shaoshaohuo.app.net.HttpRequest;
import com.shaoshaohuo.app.utils.DeviceHelper;
import com.shaoshaohuo.app.utils.ToastUtil;

/**
 * @author yusonglin
 * @ClassName: BaseActivity
 * @说明:base activity
 */

public class BaseActivity extends AppCompatActivity {

    private Dialog mDialog;
    private Handler handler;
    private Runnable runnable;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setBehindContentView(new View(this));
//        getSlidingMenu().setSlidingEnabled(false);
        ActivityCollector.addActivity(this);
        Log.i("BaseActivity", getClass().getSimpleName());
    }

    public void showToast(String text) {
        ToastUtil.toast(text);
    }

    public void showToast(int res) {
        ToastUtil.toast(res);
    }

    public void startLoadingDialog() {
        if (handler == null) {
            handler = new Handler();
        }
        if (runnable == null) {
            runnable = new Runnable() {

                @Override
                public void run() {
                    if (mDialog == null) {
                        mDialog = new Dialog(BaseActivity.this, R.style.dialog);
                        View contentView = LayoutInflater.from(BaseActivity.this).inflate(R.layout.layout_loadingdialog, null);
                        mDialog.setContentView(contentView);
                        Window window = mDialog.getWindow();
                        window.setWindowAnimations(R.style.customDialog_anim_style);
                        mDialog.setCanceledOnTouchOutside(false);
                        mDialog.setCancelable(true);
                    }

                    mDialog.show();
                }
            };
        }
        handler.postDelayed(runnable, 1000);
    }

    public void dismissLoadingDialog() {
        if (handler != null && runnable != null) {
            handler.removeCallbacks(runnable);
            handler = null;
            runnable = null;
            if (mDialog != null) {
                mDialog.dismiss();
            }
        }
    }

    @Override
    protected void onDestroy() {
        DeviceHelper.hideIME(getWindow().getDecorView());
        dismissLoadingDialog();
        super.onDestroy();
        ActivityCollector.removeActivity(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        HttpRequest.requestQueue.cancelAll(this);
    }

    /**
     * 关闭软键盘
     */
    protected void hideSoftInput() {
        View view = getWindow().peekDecorView();
        if (view != null) {
            InputMethodManager inputmanger = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            inputmanger.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }

    /**
     * 获取状态栏的高度
     *
     * @param context
     * @return
     */
    public static int getStatusBarHeight(Context context) {
        // 反射手机运行的类：android.R.dimen.status_bar_height.
        return getSystemComponentDimen(context, "status_bar_height");
    }

    private static int getSystemComponentDimen(Context context, String dimenName) {
        // 反射手机运行的类：android.R.dimen.status_bar_height.
        int statusHeight = -1;
        try {
            Class<?> clazz = Class.forName("com.android.internal.R$dimen");
            Object object = clazz.newInstance();
            String heightStr = clazz.getField(dimenName).get(object).toString();
            //id
            int height = Integer.parseInt(heightStr);
            //dp--->px
            statusHeight = context.getResources().getDimensionPixelSize(height);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return statusHeight;
    }


    public enum TransltestausModel {
        TOPPicter,
        NoePicter
    }

    /**
     * 沉浸式设计
     * <p>
     * TOPPicter,顶部有图片的沉浸
     * NoePicter，普通沉浸
     * 只支持4.4以上沉浸
     *
     * @toolbar 最外层父布局
     */
    public void changeTransltestaus(View toolbar, Activity context, TransltestausModel modle, int color) {
        if (toolbar == null) {
            return;
        }

        if (TransltestausModel.TOPPicter == modle) {

            //设置透明状态栏
            context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //顶部图片模式，这个模式需要设置图片位于最顶部，所以应该设置为透明状态栏以及置顶的view为FitsSystemWindows(true)

        } else {

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP) {
                //4.4-5.0需要处理
                //设置透明状态栏
//                Log.d(TAG, "changeTransltestaus: ");
//                Toast.makeText(context, "方案一", Toast.LENGTH_LONG).show();
                context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                toolbar.setFitsSystemWindows(true);
                ViewGroup.LayoutParams layoutParams = toolbar.getLayoutParams();
                if (layoutParams == null) {
                    return;
                }
                toolbar.setPadding(
                        toolbar.getPaddingLeft(),
                        toolbar.getPaddingTop() + getStatusBarHeight(context),
                        toolbar.getPaddingRight(),
                        toolbar.getPaddingBottom());
                //设置顶部的颜色
//                toolbar.setBackgroundColor(color);
                /**
                 * 改变下策略我们在conten上加上一个跟starbar一样高度的空间，颜色设置为想要的颜色，是不是就能解决这个问题
                 */
                ViewGroup content = (ViewGroup) context.findViewById(android.R.id.content);
                View satecolorview = new View(context);
                ViewGroup.LayoutParams layoutParams1 = new ViewGroup.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, getStatusBarHeight(context));
                satecolorview.setLayoutParams(layoutParams1);
                content.addView(satecolorview);
                satecolorview.setBackgroundColor(color);

            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//                Toast.makeText(context, "方案二", Toast.LENGTH_LONG).show();
                context.getWindow().setStatusBarColor(color);

            }
        }


    }

    /**
     * drawblelayout的解决方案
     */
    public void changestate(Activity context, DrawerLayout drawerLayout, int color) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            /**
             * 4.4以上都需要状态栏透明
             */
            if (drawerLayout == null && drawerLayout instanceof DrawerLayout) {
                return;
            }
            //设置透明状态栏
            context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            /**
             * drawblelayout就两个子类我这边就偷懒了
             */
            //正常布局
            ViewGroup childAt = (ViewGroup) drawerLayout.getChildAt(0);
            //同时要求是线性布局，如果不是你这个怎么添加= =。没得添加我这边就不做手脚了自己套个线性就行了
            View satecolorview = new View(context);
            ViewGroup.LayoutParams layoutParams1 = new ViewGroup.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, getStatusBarHeight(context));
            satecolorview.setLayoutParams(layoutParams1);
            childAt.addView(satecolorview, 0);
            satecolorview.setBackgroundColor(color);
            //侧滑布局
            View childAt1 = drawerLayout.getChildAt(1);
            childAt1.setFitsSystemWindows(false);
            childAt1.setPadding(
                    childAt.getPaddingLeft(),
                    childAt.getPaddingTop() + getStatusBarHeight(this ),
                    childAt.getPaddingRight(),
                    childAt.getPaddingBottom());


        }
    }

    /**
     * 适配项目的新方案
     */
    public void changeTransltestaus(Activity activity, View ParentView, TransltestausModel modle, int color) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            boolean b = ((RelativeLayout) ParentView).getChildAt(0).getId() == 4;
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            if (!b) {
                View satecolorview = new View(activity);
                satecolorview.setId(4);
                ViewGroup.LayoutParams layoutParams1 = new ViewGroup.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, getStatusBarHeight(activity));
                satecolorview.setLayoutParams(layoutParams1);
                ((RelativeLayout) ParentView).addView(satecolorview, 0);
            }
            if (modle == TransltestausModel.TOPPicter) {
                //頂部模式
                //顶部透明
                ((RelativeLayout) ParentView).getChildAt(0).setVisibility(View.GONE);
                RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) ((RelativeLayout) ParentView).getChildAt(1).getLayoutParams();
                layoutParams.setMargins(0, 0, 0, 0);
            } else {
                ((RelativeLayout) ParentView).getChildAt(0).setVisibility(View.VISIBLE);
                RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) ((RelativeLayout) ParentView).getChildAt(1).getLayoutParams();
                layoutParams.setMargins(0, getStatusBarHeight(activity), 0, 0);
//                ((RelativeLayout) ParentView).getChildAt(0).setAlpha(1);
                ((RelativeLayout) ParentView).getChildAt(0).setBackgroundColor(color);
            }
        }


    }
}
